package com.ycx.furns.web;

import com.ycx.furns.entity.Furn;
import com.ycx.furns.entity.Page;
import com.ycx.furns.service.FurnService;
import com.ycx.furns.service.impl.FurnServiceImpl;
import com.ycx.furns.utils.DataUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/manage/furnServlet")
public class FurnServlet extends BasicServlet{
    FurnService furnService = new FurnServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        System.out.println("page拿到请求参数：" + pageNo);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        Page<Furn> page = furnService.getPage(pageNo, pageSize);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/manage/furn_manage.jsp").forward(req, resp);
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Furn furn = new Furn();
            BeanUtils.populate(furn, req.getParameterMap());
            boolean b = furnService.updateFurn(furn);
            if(b){
                System.out.println("修改成功");
            }
//            req.getSession().setAttribute("pageNo", req.getAttribute("pageNo") );
            resp.sendRedirect("/jiaju_mall/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
//            req.getRequestDispatcher("/manage/furnServlet?action=page" ).forward(req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void showUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Furn furn = furnService.getFurnById(Integer.parseInt(id));
        req.setAttribute("furn", furn);
        String pageNo = req.getParameter("pageNo");
        System.out.println("拿到请求参数：" + pageNo);
        req.setAttribute("pageNo", pageNo);
        req.getRequestDispatcher("/views/manage/furn_update.jsp"  ).forward(req, resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if(furnService.delFurn(Integer.parseInt(id))){
            System.out.println("删除成功");
            resp.sendRedirect("/jiaju_mall/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
        }else {
            System.out.println("删除失败");
        }
    }

    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Furn> furns = furnService.queryFurns();
        for (Furn furn : furns) {
            System.out.println(furn);

        }
        req.setAttribute("furnList" , furns);
        req.getRequestDispatcher("/views/manage/furn_manage.jsp").forward(req, resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String maker = req.getParameter("maker");
        String price = req.getParameter("price");
        String sales = req.getParameter("sales");
        String stock = req.getParameter("stock");
        String img_path = "assets/images/product-image/default.jpg";
        Furn furn = null;
        try {
            furn = new Furn(null, name, maker, new BigDecimal(price), Integer.parseInt(sales),
                    Integer.parseInt(stock), img_path);
        } catch (NumberFormatException e) {
            req.setAttribute("furn_msg", "添加数据格式不对");
            req.getRequestDispatcher("/views/manage/furn_add.jsp").forward(req, resp);
            throw new RuntimeException(e);
        }
        System.out.println(furn);
        boolean b = furnService.addFurn(furn);

        if(b){
            System.out.println("添加成功");
//            req.getRequestDispatcher("/manage/furnServlet?action=list").forward(req, resp);
            resp.sendRedirect("/jiaju_mall/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
        }else {
            System.out.println("添加失败");
//            req.getRequestDispatcher("/views/manage/furn_add.jsp").forward(req, resp);
            resp.sendRedirect("/jiaju_mall/views/manage/furn_add.jsp" + req.getParameter("pageNo"));
        }
    }
}
